package com.cloud.lan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.lan.api.dto.converter.TestConverter;
import com.cloud.lan.api.dto.req.TestModelReq;
import com.cloud.lan.api.dto.resp.TestModelResp;
import com.cloud.lan.dao.TestModelMapper;
import com.cloud.lan.entity.base.BaseRequest;
import com.cloud.lan.entity.base.BaseResponse;
import com.cloud.lan.entity.base.PageRequest;
import com.cloud.lan.entity.base.PageResponse;
import com.cloud.lan.entity.base.common.PageUtil;
import com.cloud.lan.entity.model.dataobject.TestModelDO;
import com.cloud.lan.service.ITestModelService;

import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

import javax.annotation.Resource;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;

/**
 * <p>
 * 测试信息表 服务实现类
 * </p>
 *
 * @author xyqierkang@163.com
 * @since 2021-12-05
 */
@Service
public class TestModelServiceImpl extends ServiceImpl<TestModelMapper, TestModelDO> implements ITestModelService {

    @Resource
    TestModelMapper testModelMapper;

    @Override
    public PageResponse<List<TestModelResp>> list(@RequestBody PageRequest<TestModelReq> request) {
        LambdaQueryWrapper<TestModelDO> qw = new LambdaQueryWrapper<>();
        TestModelReq req = request.getRequestData();
        if (StrUtil.isNotEmpty(req.getName())) {
            qw.like(TestModelDO::getName, req.getName());
        }
        Page<TestModelDO> page = new Page<>(request.getPage().getCurrentPage(), request.getPage().getPageSize());
        Page<TestModelDO> pageList = testModelMapper.selectPage(page, qw);
        PageResponse<List<TestModelResp>> responseList = PageUtil.toPageResp(pageList, TestConverter.INSTANCE::convertTo);
        return responseList;
    }

    @Override
    public BaseResponse<Object> add(BaseRequest<TestModelReq> request) {
        TestModelDO modelReq = Convert.convert(TestModelDO.class, request.getRequestData());
        testModelMapper.insert(modelReq);
        return BaseResponse.success(modelReq);
    }

    @Override
    public BaseResponse<Object> update(BaseRequest<TestModelReq> request) {
        return null;
    }

    @Override
    public BaseResponse<Object> delete(BaseRequest<TestModelReq> request) {
        return null;
    }
}
